package com.blb.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.ZyUnit;
import com.blb.community.entity.vo.UnitVO;
import com.blb.community.service.IZyUnitService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 单元  前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "单元信息")
@RestController
@RequestMapping("/zy-unit")
public class ZyUnitController {

    @Autowired
    private IZyUnitService zyUnitService;

    @ApiOperation("单元分页查询")
    @PostMapping("units/page")
    public ResponseResult<IPage<UnitVO>> getListPage(@RequestBody UnitVO unitVO){
        IPage<UnitVO> listPage = zyUnitService.getListPage(unitVO,new Page<UnitVO>(unitVO.getCurrent(), 5));
        return ResponseResult.ok(listPage);
    }
    @Log(module = "添加单元", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加单元")
    @PutMapping("unit")
    public ResponseResult<String> saveUnit(@RequestBody UnitVO unitVO){
        String s = zyUnitService.saveUnit(unitVO);
        if (s == "添加成功"){
            return ResponseResult.ok("添加成功");
        }
        return ResponseResult.ok("添加失败");
    }
    @Log(module = "修改单元", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改单元")
    @PostMapping("unit")
    public ResponseResult<String> update(@RequestBody UnitVO unitVO){
        zyUnitService.updateById(unitVO);
        return ResponseResult.ok("ok");
    }
    @Log(module = "删除单元", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除单元")
    @DeleteMapping("unit/{unitId}")
    public ResponseResult<String> deleteUnit(@PathVariable long unitId){
        String s = zyUnitService.deleteUnit(unitId);
        if (s == "删除成功"){
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.ok("删除失败");
    }
    @Log(module = "多删单元", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("多删")
    @DeleteMapping("unitAll/{unitIds}")
    public ResponseResult<String> deleteAll(@PathVariable List<Long> unitIds){
        String s = zyUnitService.deleteUnitAll(unitIds);
        if (s == "删除成功"){
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.ok("删除失败");
    }

    /**
     * 根据buildingName查询对应单元
     * @param buildingName
     * @return
     */
    @GetMapping("/{buildingName}")
    public ResponseResult<List<ZyUnit>> selectZyUnitByBuildingId(@PathVariable String buildingName) {
        List<ZyUnit> zyUnits = zyUnitService.selectUnit(buildingName);
        return ResponseResult.ok(zyUnits);
    }
}
